/*
 * gs_single.c
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <minigui/common.h>
#include <minigui/minigui.h>
#include <minigui/gdi.h>
#include <minigui/window.h>
#include <minigui/control.h>

#include "gs_single.h"

typedef enum 
{
    GS_BST_NORMAL,
    GS_BST_HLIGHT,
    GS_BST_PUSHED,
} GS_BUTTONSTAT;

typedef struct _GS_BUTTONDATA
{
    PBITMAP data;
    GS_BUTTONSTAT  stat;
} GS_BUTTONDATA;
typedef GS_BUTTONDATA* PGS_BUTTONDATA;

static void draw_button (HWND hWnd, PGS_BUTTONDATA pData)
{
    HDC hdc;
    RECT rect;
    
    hdc = BeginPaint (hWnd);
    GetClientRect (hWnd, &rect);

    switch (pData->stat)
    {
        case GS_BST_PUSHED:
            Rectangle (hdc, rect.left + 1, rect.top + 1, 
                    rect.right - 2, rect.bottom - 2);
            SetBrushColor (hdc, PIXEL_red); 
            FillBox (hdc, rect.left + 1, rect.top + 1, 
                    rect.right - 2, rect.bottom - 2);
            break;
        case GS_BST_HLIGHT:
            Rectangle (hdc, rect.left + 1, rect.top + 1, 
                    rect.right - 2, rect.bottom - 2);
            break;
        default:
            break;
    }

    if (pData->data) {
        LOGFONT* logfont;
        SIZE size;
        int x, y;
        int oldmode;
        const char * caption = GetWindowCaption (hWnd);
        
        logfont = CreateLogFont (NULL, "song", "GB2312",
                FONT_WEIGHT_REGULAR, FONT_SLANT_ROMAN, FONT_SETWIDTH_NORMAL,
                FONT_SPACING_CHARCELL, FONT_UNDERLINE_NONE, FONT_STRUCKOUT_NONE,
                12, 0);
        SelectFont (hdc, logfont); 
        GetTextExtent (hdc, caption, -1, &size);
        
        oldmode = SetBkMode (hdc, BM_TRANSPARENT);
        
        x = (int) (RECTW(rect) - pData->data->bmWidth) / 2;
        y = (int) (RECTH(rect) - pData->data->bmHeight -  size.cy) / 2;
        FillBoxWithBitmap (hdc, x, y, -1, -1, (PBITMAP) pData->data);
        
        x = (int) (RECTW(rect) - size.cx) / 2;
        y = (int) y + pData->data->bmHeight;
        TextOut (hdc, x, y, caption);
        
        oldmode = SetBkMode (hdc, oldmode);
    }


    EndPaint (hWnd, hdc);
}

static int GsSingleProc (HWND hWnd, int message, WPARAM wParam, LPARAM lParam)
{
    PGS_BUTTONDATA pData = NULL;
    
    if (message != MSG_CREATE)
        pData = (PGS_BUTTONDATA) GetWindowAdditionalData2 (hWnd);
            
    switch (message) 
    {
        case MSG_CREATE:
        {
            DWORD userdata;
            
            pData = (PGS_BUTTONDATA) calloc (1, sizeof (GS_BUTTONDATA));
            
            if (pData == NULL) return -1;

            userdata = GetWindowAdditionalData(hWnd);
            
            if (userdata) {
                pData->data = (PBITMAP)userdata;
            }
            else {
                pData->data = NULL;
            }
            
            pData->stat = GS_BST_NORMAL;
            
            SetWindowAdditionalData2 (hWnd, (DWORD)pData);

            break; 
        }
        case MSG_DESTROY: 
            free ((void*) pData);
            break;

        case MSG_PAINT:
            draw_button (hWnd, pData);
            break;

        case MSG_MOUSEMOVEIN: 
        {
            BOOL in_out = (BOOL) wParam;
            
            if (in_out) 
                pData->stat = GS_BST_HLIGHT;
            else 
                pData->stat = GS_BST_NORMAL;
            InvalidateRect(hWnd, NULL, TRUE);
            
            break;
        }
        case MSG_LBUTTONDOWN: 
            
            pData->stat = GS_BST_PUSHED;
            InvalidateRect(hWnd, NULL, TRUE);
            
            printf ("Into MSG_LBUTTONDON\n");  
            break;

        case MSG_LBUTTONUP: 
            pData->stat = GS_BST_HLIGHT;
            InvalidateRect(hWnd, NULL, TRUE);
            //NotifyParentEx (hWnd, GetDlgCtrlID(hWnd), GS_BN_CLICKED, 0);
            printf ("Into MSG_LBUTTONUP\n");  
            break;
    }

    return DefaultControlProc (hWnd, message, wParam, lParam);
}

BOOL RegisterGsSingle (void)
{
    WNDCLASS WndClass;

    WndClass.spClassName = GS_SINGLE;
    WndClass.dwStyle     = WS_NONE;
    WndClass.dwExStyle   = WS_EX_NONE;
    WndClass.hCursor     = GetSystemCursor (IDC_ARROW);
    WndClass.iBkColor    = GetWindowElementColor (BKC_CONTROL_DEF);
    WndClass.WinProc     = GsSingleProc;

    return RegisterWindowClass (&WndClass);
}

void UnregisterGsSingle (void)
{
    UnregisterWindowClass (GS_SINGLE);
}


